<?php
	
	/**
	 * PkCache File.
	 * 
	 * @author Brian Bal <brian@youbeqb.com>
	 * @version 1.0
	 * @package pink-koala
	 */
	
	/**
	 * PkCache class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 **/
	
	class PkCache
	{
		public function getCacheId($name)
		{
			return md5($name);
		}
		
		public function checkCache($name, $time_limit)
		{
			$needs_render = true;
			
			$id = $this->getCacheId($name);
			$cache_filename = BASE_DIR . "system/tmp/cache/$id.cache";
			
			if(file_exists($cache_filename))
			{
				$time_dif = filemtime($cache_filename) - time();
				if($time_limit > $time_dif)
				{
					$needs_render = false;
				}
			}
			
			return $needs_render;
		}
		
		public function setCache($name, $content)
		{
			$id = $this->getCacheId($name);
			$cache_filename = BASE_DIR . "system/tmp/cache/$id.cache";
			
			if (!$handle = @fopen($cache_filename, 'w'))
			{
		         echo "Cannot open file ($cache_filename)";
		    }
			else
			{
				if (fwrite($handle, $content) === FALSE)
				{
					echo "Cannot write to file ($cache_filename)";
				}
				fclose($handle);
			}
		}
		
		public function getCache($name)
		{
			$id = $this->getCacheId($name);
			$cache_filename = BASE_DIR . "system/tmp/cache/$id.cache";
			$cache_content = "";
			
			$handle = @fopen($cache_filename, "r");
			if ($handle)
			{
				while (!feof($handle))
				{
					$cache_content .= fgets($handle, 4096);
				}
				fclose($handle);
			}
			
			return $cache_content;
		}
	}
	
?>